<template>
  <div class="loginContainer">
    <el-card header="修改密码">
      <el-form
        :model="formData"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        label-position="left"
        :data="tabledata"
      >
        <el-form-item label="旧密码：" prop="oldPassword">
          <el-input
            type="text"
            v-model="formData.oldPassword"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="Password">
          <el-input
            type="password"
            v-model="formData.Password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="确认密码：" prop="confirmPassword">
          <el-input
            type="password"
            v-model="formData.confirmPassword"
            autocomplete="off"
          >
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >

          <el-button type="primary" @click="returnDefault">返回首页</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getList, modUser } from "../api/users";
import { clearToken } from "../utils/auth";
import Cookies from "js-cookie";
export default {
  data() {
    return {
      tabledata: [],
      pager: {
        pageIndex: 1, // 页码
        pageSize: 10, // 页面大小（每页项目的数量）
        rowsTotal: 30, // 总记录数
      },
      formData: {
        id: 0,
        username: "",
        oldPassword: "",
        Password: "",
        confirmPassword: "",
      },
      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
      },
      rules: {
        oldPassword: [
          { required: true, message: "请输入你的密码！", trigger: "blur" },
        ],
        Password: [
          { required: true, message: "请输入你的密码！", trigger: "blur" },
        ],
        confirmPassword: [
          { required: true, message: "请再次输入你的密码！", trigger: "blur" },
        ],
      },
      currentIndex: 1,
    };
  },
  methods: {
    submitForm(formName) {
      let ModUser = {
        Username: this.uName,
        Password: this.formData.Password,
      };

      console.log(ModUser);
      console.log(this.formData.password);

      if (this.formData.oldPassword !== this.formData.Password) {
        if (this.formData.Password === this.formData.confirmPassword) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              modUser(this.uId, ModUser).then((res) => {
                console.log(res);
                this.tableData.splice(5, 1, res.data);
                this.$message.success("修改成功！请重新登录");
                Cookies.remove("username");
                Cookies.remove("id");
                clearToken();
                this.$router.push("/test")
              });
            }
          });
        } else {
          console.log("error submit!!");
          this.$message.error("两次密码不相同！");
          return false;
        }
      } else {
        this.$message.error("新密码不能与旧密码相同！");
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    returnDefault() {
      this.$router.push("/newIndex");
    },
    // 拉取数据方法
    fetchData(pager) {
      getList(pager)
        .then(({ data }) => {
          console.log(data);
          let res = data.data;
          this.tableData = res.data;
          this.pager = res.pager;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  computed: {
    uId() {
      return Cookies.get("id");
    },
    uName() {
      return Cookies.get("username");
    },
  },
  mounted() {
    this.fetchData(this.pager);
  },
};
</script>

<style>
body {
  background-color: white;
}
.loginContainer {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 50vh;
  font-weight: 900;
}
.el-card__header {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 26px;
  font-weight: 900;
}
.el-card__body {
  width: 400px;
  /* background-image: url("./imgs/BackImg.png");
  opacity: 0.7; */
}
</style>